package model.util.stats;

import java.util.List;

import org.apache.commons.math.MathException;
import org.apache.commons.math.stat.regression.SimpleRegression;

public class RegressionCalc {

	public class RegressionResult{
		private double alpha=0;
		private double beta=0;
		private double rsquared=0;
		private double standardError;
		public void setStandardError(double standardError) {
			this.standardError = standardError;
		}
		public double getStandardError() {
			return standardError;
		}
		public double getAlpha() {
			return alpha;
		}
		public void setAlpha(double alpha) {
			if(!Double.isNaN(alpha)){
			
				this.alpha = alpha;
			}
		}
		public double getBeta() {
				
			return beta;
			
		}
		public void setBeta(double beta) {
			if(!Double.isNaN(beta)){
				
		
			this.beta = beta;
			}
		}
		public double getRsquared() {
				
			return rsquared;
			
		}
		public void setRsquared(double rsquared) {
			if(!Double.isNaN(rsquared)){
				
			this.rsquared = rsquared;
			}
		}
	}
	
	public RegressionResult run(List<Double> asset,List<Double> index ){
		//AM: first convert the lists into double[][]..
		double[][] params =new double[index.size()][2];
		for(int i=0;i<index.size();i++){
			params[i][1]=asset.get(i);
			params[i][0]=index.get(i);
		}
		SimpleRegression regression = new SimpleRegression();
		regression.addData(params);
		RegressionResult result= new RegressionResult();
		result.setAlpha(regression.getIntercept());
		result.setBeta(regression.getSlope());
		result.setRsquared(regression.getRSquare());
		result.setStandardError(regression.getSlopeStdErr());
		
		//AMLOW: maybe play with this..
//		try {
//			regression.getSignificance();
//		} catch (MathException e) {
//			throw new RuntimeException(e);
//		}
		
		return result;
	}
}
